//常用自定义检验规则
export function checkMobile(value) {
    let reg = /^1(3\d|4[5-8]|5[0-35-9]|6[6]|7[2-8]|8\d|9[0-35-9])\d{8}$/
    if (!reg.test(value)) {
        return false;
    } else {
        return true
    }
}
//校验车牌号
export function checkPlateNo(value) {
    let reg1 = /^[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
    let reg2 = /^[A-Z]{1}(([0-9]{5}[DF])|([DF][A-HJ-NP-Z0-9][0-9]{4}))$/;
    return reg1.test(value) || reg2.test(value);
}
//进行经纬度转换为距离的计算
function Rad(d) {
    return (d * Math.PI) / 180.0; //经纬度转换成三角函数中度分表形式。
}

/*
计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
默认单位km
*/
export function getMapDistance(lat1, lng1, lat2, lng2) {
    var radLat1 = Rad(lat1);
    var radLat2 = Rad(lat2);
    var a = radLat1 - radLat2;
    var b = Rad(lng1) - Rad(lng2);
    var s =
        2 *
        Math.asin(
            Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) *
                Math.cos(radLat2) *
                Math.pow(Math.sin(b / 2), 2)
            )
        );
    s = s * 6378.137; // EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000; //输出为公里
    //s=s.toFixed(2);
    return s;
}
//重复点击处理
var lastTime = 0
export function isFastClick() {
    let time = new Date().getTime();
    if (time - lastTime < 500) {
        console.log("重复点击");
        return true;
    }
    lastTime = time;
    return false;
}
//获取opnedId
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });
import Taro from "@tarojs/taro";
const apiBaseUrl = process.env.IOParking.API_HOST; //请求头部
const openIdMock = "oRPxj5bi2JB-id96cJ_s1wBWmjAo";
const mobileMock = "18039670989";
function login() {
    return new Promise((resolve, reject) => {
        api
            .getUserInfo({
                scope: "all",
            })
            .then((resp) => {
                console.log("微信authCode is：", JSON.stringify(resp));
                if (resp.success) {
                    if (resp.data == "mock") {
                        Taro.setStorage({
                            key: "openId",
                            data: openIdMock,
                        });
                        Taro.setStorage({
                            key: "mobile",
                            data: mobileMock,
                        });
                        resolve("555555555");
                        return;
                    }
                    resolve(resp.data);
                } else {
                    console.log("err is ", resp.message, "err code is ", resp.code);
                    reject(resp.message);
                }
            });
    });

}
export function wx_login() {
    return new Promise((resolve, reject) => {
        login().then((res) => {
            if (res == null) {
                resolve();
            } else {
                Taro.request({
                    url: apiBaseUrl + "/weixin/souchegetuser22?authCode=" + res,
                    data: {},
                    header: {
                        "content-type": "application/json", // 默认值
                    },
                    method: "GET",
                    success: (res) => {
                        console.log("获取用户信息：", res);
                        if (res.data.isSuccess) {
                            var userOpenId = res.data.data.openId;
                            let mobileRes = res.data.data.phoneNum;
                            if (userOpenId) {
                                Taro.setStorage({
                                    key: "openId",
                                    data: userOpenId,
                                });
                            }
                            if (mobileRes) {
                                Taro.setStorage({
                                    key: "mobile",
                                    data: mobileRes,
                                });
                            }
                            resolve();
                        } else {
                            reject(res.data.statusCode);
                        }
                    },
                    fail(){
                        console.log("失败------");
                        reject(error);
                    }
                });
            }
        });
    });

}
